<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>generator</title>
</head>

<body>
  <script>
    'use strict'
    onload = function () {
      /* 
        1.Generator 函数是一个状态机，封装了多个内部状态。
        2.调用generator函数后返回一个iterator对象,每次调用iterator对象上的next方法,可以执行一个yield后的状态
      */

      function* generator() {
        yield setTimeout(() => {
          console.log("A成功");
          gen.next();
        }, 2000)
        yield setTimeout(() => {
          console.log("B成功");
          gen.next();
        }, 2000)
        yield setTimeout(() => {
          console.log("c成功");
        }, 2000)
      }

      const gen = generator()
      gen.next()
    }
  </script>
</body>

</html>